package es.cazadoresmadridejos.beans.controller.adviced;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import es.cazadoresmadridejos.beans.manager.adviced.AdvicedManager;
import es.cazadoresmadridejos.beans.manager.reason.ReasonManager;
import es.cazadoresmadridejos.beans.validator.adviced.AdvicedValidator;
import es.cazadoresmadridejos.model.Adviced;
import es.cazadoresmadridejos.model.Reason;
import es.cazadoresmadridejos.propertyeditor.AdvicedPropertyEditor;
import es.cazadoresmadridejos.util.FileUtils;
import es.cazadoresmadridejos.util.FileUtils.FileType;
import es.cazadoresmadridejos.util.constants.Constants;
import es.cazadoresmadridejos.util.model.ReasonUtil;
import es.cazadoresmadridejos.util.validation.ValidationContainer;

@Controller
public class AdvicedCreateModifyController {

	@Autowired private AdvicedManager advicedManager;
	@Autowired private AdvicedValidator advicedValidator;
	@Autowired private MessageSource messageSource;
	@Autowired private ReasonManager reasonManager;

	@InitBinder
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Throwable{
		binder.registerCustomEditor(Adviced.class, new AdvicedPropertyEditor(advicedManager));
		binder.registerCustomEditor(Date.class, "date",new CustomDateEditor(Constants.sdf,true));
	}

	@RequestMapping(value="/advicedCreateModify.html",method=RequestMethod.GET)
	public ModelAndView createModify(
			@RequestParam(value="id",required=false) Adviced adviced) throws Throwable {
		ModelAndView mv = new ModelAndView();
		if(adviced == null){
			adviced = new Adviced();
			mv.addObject("type","create");
		}else{
			mv.addObject("type","update");
		}
		if(adviced.getText() != null){
			adviced.setText(adviced.getText().replaceAll("\r", "").replaceAll("\n","").replaceAll("\"", "'"));
		}
		
		mv.addObject("adviced",adviced);


		return mv;
	}
	@RequestMapping(value="/advicedCreateModify.html",method=RequestMethod.POST)
	public ModelAndView submit(
			@ModelAttribute("adviced") Adviced adviced, 
			BindingResult result, 
			SessionStatus status,
			HttpServletRequest request
			) throws Throwable {

		ModelAndView mv = new ModelAndView("redirect:index.html");
		mv.addObject("screen","advicedList.html");
		
		List<ValidationContainer> errors = advicedValidator.validate(adviced);
		if(errors.isEmpty()){
			mv.addObject("okMessage", "THE_ADVICED_WAS_CREATED_SUCCESSFULLY");
		}else{
			mv.addObject("errors",errors);
		}
		
		Reason reason = reasonManager.getByReason(ReasonUtil.getReasonFromDate(adviced.getDate()));
		if(reason == null){
			reason = new Reason();
			reason.setYear(ReasonUtil.getReasonFromDate(adviced.getDate()));
			reasonManager.save(reason);
		}
		
		adviced.setReason(reason);

		String name = FileUtils.saveForm(adviced.getFile(), FileType.ADVICED);
		adviced.setImageUrl(name);

		advicedManager.save(adviced);
		
		return mv;
	}
}
